package
{
   import _effx.Rippler;
   
   import flash.display.Bitmap;
   import flash.display.BitmapData;
   import flash.display.MovieClip;
   import flash.display.StageAlign;
   import flash.display.StageScaleMode;
   import flash.events.Event;
   import flash.events.MouseEvent;

   public class WaterRippleMain extends MovieClip
   {
      // fla
      public var mcBg:MovieClip;

      // necessary properties
      private const CANVAS_WIDTH:Number = 625;
      private const CANVAS_HEIGHT:Number = 450;
      private var canvasBmp:Bitmap;
      private var rippler:Rippler;
      
      // debugger
      private var buff1:Bitmap = new Bitmap();
      private var buff2:Bitmap = new Bitmap();
      private var finalBuff:Bitmap = new Bitmap();

      public function WaterRippleMain()
      {
         super();

         addEventListener(Event.ADDED_TO_STAGE, onAdd);
         addEventListener(Event.REMOVED_FROM_STAGE, onRemove);
      }

      // --------------------- LINE ---------------------

      // ################### protected ##################

      // #################### private ###################

      private function onAdd(e:Event):void
      {
         // basic
         stage.scaleMode = StageScaleMode.NO_SCALE;
         stage.align = StageAlign.TOP_LEFT;

         gotoAndPlay(1);
         
         // Rippler only accept the bitmap resource
         canvasBmp = new Bitmap(new BitmapData(CANVAS_WIDTH, CANVAS_HEIGHT, false, 0xffffff));
         canvasBmp.visible = false;
         addChild(canvasBmp);
         
         rippler = new Rippler(canvasBmp, 200, 10);
         rippler.onUpdate = renderIt; // call onUpdate() before the renderering every frame.
         rippler.onComplete = renderComplete; // call onComplete() once when it finished.
         
         // mouse interaction
         stage.addEventListener(MouseEvent.MOUSE_DOWN, onMDown);
         stage.addEventListener(MouseEvent.CLICK, createRippler);
         
         // inspector
         buff1.x = 2 * CANVAS_WIDTH + 5; buff1.y = 5;
         buff2.x = 2 * CANVAS_WIDTH + 5; buff2.y = 60; 
         finalBuff.x = CANVAS_WIDTH; finalBuff.y = 0;
         addChild(buff1);
         addChild(buff2);
         addChild(finalBuff);
         addEventListener(Event.ENTER_FRAME, updateInspector);
      }
      
      private function onRemove(e:Event):void
      {
      }

      // --------------------- LINE ---------------------
      
      private function onMDown(e:MouseEvent):void
      {
         createRippler();
         stage.addEventListener(MouseEvent.MOUSE_MOVE, createRippler);
         stage.addEventListener(MouseEvent.MOUSE_UP, onMUp);
      }
      
      private function onMUp(e:MouseEvent):void
      {
         stage.removeEventListener(MouseEvent.MOUSE_MOVE, createRippler);
         stage.removeEventListener(MouseEvent.MOUSE_UP, onMUp);
      }
      
      private function createRippler(e:MouseEvent = null):void
      {
         rippler.drawRipple(stage.mouseX, stage.mouseY, 20, 1);
      }
      
      // --------------------- LINE ---------------------

      private function renderIt():void
      {
         canvasBmp.visible = false;

         canvasBmp.bitmapData.draw(this);

         canvasBmp.visible = true;
      }

      private function renderComplete():void
      {
         canvasBmp.visible = false;
      }
      
      // --------------------- LINE ---------------------
      
      private function updateInspector(e:Event):void
      {
         buff1.bitmapData = rippler.buffer1Image;
         buff2.bitmapData = rippler.buffer2Image;
         finalBuff.bitmapData = rippler.rippleImage;
      }

      // --------------------- LINE ---------------------

   }

}